/**
 * 
 */
package org.opensource.yabts.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.opensource.yabts.client.LoginForm;
import org.opensource.yabts.client.LoginModalForm;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

	protected static Logger logger = Logger.getLogger("security");
	
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {

			logger.debug("Authentication required");
			
		 	HttpServletResponse httpResponse = (HttpServletResponse) response;
	        httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication required");
	        
//	        createModalWindow();
	}
	
	private void createModalWindow() {
		final Window winModal = new Window();
		
        winModal.setWidth(360);
        winModal.setHeight(115);
        winModal.setTitle("Modal Window");
        winModal.setShowMinimizeButton(false);
        winModal.setIsModal(true);
        winModal.setShowModalMask(true);
        winModal.centerInPage();
        DynamicForm form = new DynamicForm();
        form.setHeight100();
        form.setWidth100();
        form.setPadding(5);
        form.setLayoutAlign(VerticalAlignment.BOTTOM);
        TextItem textItem = new TextItem();
        textItem.setTitle("Text");
        DateItem dateItem = new DateItem();
        dateItem.setTitle("Date");
        DateItem dateItem2 = new DateItem();
        dateItem2.setTitle("Date");
        dateItem2.setUseTextField(true);
        form.setFields(textItem, dateItem, dateItem2);
        winModal.addItem(form);
        winModal.show();
	}

}
